#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
const QString SEVER_IP = "127.0.0.1";
const qint16 SEVER_PORT = 9090;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("客户端");

    socket = new QTcpSocket(this);

    socket->connectToHost(SEVER_IP, SEVER_PORT);

    connect(socket, &QTcpSocket::readyRead, this, [=](){
        QString response = socket->readAll();
        ui->listWidget->addItem("服务器说:" + response);
    });

    bool ret = socket->waitForConnected();
    if(!ret)
    {
        QMessageBox::critical(this, "连接服务器出错", socket->errorString());
        exit(1);
    }
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    const QString& text = ui->lineEdit->text();

    socket->write(text.toUtf8());

    ui->listWidget->addItem("客户端说:" + text);

    ui->lineEdit->setText("");
}
